package com.mk.web.structure.search;

/**
 * 顺序查找
 * Created by WangChen on 2018/12/18 17:19.
 */
public class SequentialSearch {


    public static void main(String [] args){
        int[] array = {6, 5, 7, 3, 8, 2};// 查找范围
        int key = 1;// 查找目标
//        for(int i = 0; i < array.length; i++){
//            if (key == array[i]){
//                System.out.println(i);
//                return;
//            }
//        }
//        System.out.println("不存在");

        int index = sequentialSearch(array, key);
        System.out.println(index == -1 ? "不存在" : index);// 输出查找结果

    }

    private static int sequentialSearch(int[] arr, int key){
        int index = -1;
        for(int i = 0; i < arr.length; i++){
            if (key == arr[i]){
                index = i;
                break;
            }
        }
        return index;
    }
}
